package manager;

/**
 * @author huangjm.2012, mohameds.2012, jaslin.wee.2012, huiwensong.2012, sheryl.tay.2012
 *
 */

import java.sql.SQLException;
import java.util.ArrayList;

import manager.database.DatabaseMgr;
import manager.entitycreator.SectionCreator;
import util.DateUtil;
import util.ValidationUtil;
import entity.Course;
import entity.Section;

public class SectionMgr {

	private DatabaseMgr dbMgr = new DatabaseMgr();
	private SectionCreator sectionCreator = new SectionCreator();

	public boolean addSection(Section section) {

		String sql = "insert into section "
				+ " (section_courseCode, section_sectionCode, section_day, section_startTime, section_endTime, section_instructor, section_venue, section_size) "
				+ " values (?, ?, ?, ?, ?, ?, ?, ?) ";

		int numRowsAffected = dbMgr.executeUpdate(sql, section.getCourse()
				.getCourseCode(), section.getSectionCode(), section
				.getDayOfWeek(), section
				.getStartTime(DateUtil.MYSQL_TIME_PATTERN), section
				.getEndTime(DateUtil.MYSQL_TIME_PATTERN), section
				.getInstructor(), section.getVenue(), section.getSize());

		return numRowsAffected > 0;
	}

	public int[] addMultipleSections(ArrayList<Section> sections) {
		String sql = "insert into section "
				+ " (section_courseCode, section_sectionCode, section_day, section_startTime, section_endTime, section_instructor, section_venue, section_size) "
				+ " values (?, ?, ?, ?, ?, ?, ?, ?) ";

		int[] numRowsAffected = { 0 };
		try {
			dbMgr.open();

			dbMgr.setStatement(sql);
			for (Section eachSection : sections) {
				dbMgr.addBatch(eachSection.getCourse().getCourseCode(),
						eachSection.getSectionCode(),
						eachSection.getDayOfWeek(),
						eachSection.getStartTime(DateUtil.MYSQL_TIME_PATTERN),
						eachSection.getEndTime(DateUtil.MYSQL_TIME_PATTERN),
						eachSection.getInstructor(), eachSection.getVenue(),
						eachSection.getSize());
			}
			numRowsAffected = dbMgr.executeBatch();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			dbMgr.close();
		}
		return numRowsAffected;
	}

	public void reset() {
		dbMgr.executeUpdate("delete from section");
	}

	public ArrayList<Section> retrieveAllSections() {
		return dbMgr
				.retrieveMultipleEntitiesForQuery(
						sectionCreator,
						"select * from section_with_course order by section_courseCode, section_sectionCode");
	}

	public Section retrieveSectionForCourseCodeAndSectionCode(
			String courseCode, String sectionCode) {

		String sql = "select * from section_with_course where section_courseCode=? and section_sectionCode =?";

		Section section = dbMgr.retrieveEntityForQuery(sectionCreator, sql,
				courseCode, sectionCode);

		ValidationUtil.checkForElement(section);

		return section;
	}

	public ArrayList<Section> retrieveSectionsForCourse(String courseCode) {

		String sql = "select * from section_with_course where section_courseCode=? order by section_sectionCode ";

		return dbMgr.retrieveMultipleEntitiesForQuery(sectionCreator, sql,
				courseCode);
	}

}
